FROM openeuler-22.03-lts-sp4:latest AS build

ARG VERSION=2.8.0-rocksdb-v8.5.3

COPY openEuler.repo /etc/yum.repos.d/openEuler.repo
COPY patch/CMakeLists_patch.diff /CMakeLists_patch.diff
COPY patch/gitmodules_patch.diff /gitmodules_patch.diff

RUN set -eux; \
    dnf install -y bzip2 gcc gcc-c++ libstdc++ libstdc++-static libstdc++-devel autoconf automake make libtool git cmake openssl-devel && dnf clean all

RUN set -eux; \
    mkdir tendis-src && \
    git config --global user.name "You" && \
    git config --global user.email "You@local" && \
    git clone -b ${VERSION} https://gitee.com/jason_elva8325-mirror/tencent-tendis tendis-src && \
    git config --global --add safe.directory /tendis-src/src/thirdparty/rocksdb/rocksdb && \
    cd tendis-src && \
    git apply /CMakeLists_patch.diff && \
    git apply /gitmodules_patch.diff && \
    git submodule update --init && \
    cmake -DCMAKE_BUILD_TYPE=Release -B build && cmake --build build

FROM openeuler-22.03-lts-sp4:latest

COPY gosu-arm64 /usr/local/bin/gosu
COPY openEuler.repo /etc/yum.repos.d/openEuler.repo
COPY config/tendisplus.conf /usr/local/etc/tendisplus.conf
COPY config/extra.conf /usr/local/etc/tendisplus-extra.conf.sample
COPY --from=build /tendis-src/build/bin/tendisplus /usr/local/bin/tendisplus
COPY --from=build /tendis-src/build/bin/ldb_tendis /usr/local/bin/ldb_tendis
COPY --from=build /tendis-src/build/bin/binlog_tool /usr/local/bin/binlog_tool

ENV GOSU_VERSION 1.17
RUN set -eux; \
    chmod +x /usr/local/bin/gosu

RUN set -eux; \
    dnf install -y shadow openssl ca-certificates binutils glibc-all-langpacks && \
    groupadd -g 70 tendis; \
    useradd -r -u 70 -g tendis -m -d /home/tendis tendis; \
    mkdir -p /var/run/tendis /var/lib/tendis /usr/local/etc/tendis && \
    chown -R tendis:tendis /var/run/tendis /var/lib/tendis /usr/local/etc/tendis && \
    chmod 2777 /var/run/tendis && \
    dnf clean all

ENV LANG en_US.UTF-8
EXPOSE 8903
VOLUME ["/var/lib/tendis", "/usr/local/etc/tendis"]

COPY entrypoint.sh /usr/local/bin/
RUN chmod 755 /usr/local/bin/entrypoint.sh; \
    ln -s /usr/local/bin/entrypoint.sh / # backwards compat

ENTRYPOINT ["entrypoint.sh"]
CMD ["tendisplus", "/usr/local/etc/tendisplus.conf"]
